package com.github.zhijieqing.socket.cluster.websocket;

import com.github.zhijieqing.socket.cluster.common.domain.SocketMsg;
import com.github.zhijieqing.socket.cluster.common.domain.TopicSocketMsg;
import com.github.zhijieqing.socket.cluster.common.manager.SocketManager;
import com.github.zhijieqing.socket.cluster.common.storage.SessionClientStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class WebsocketConfiguration {
    @Autowired
    private SocketManager socketManager;

    @PostConstruct
    public void postConstruct(){
        socketManager.addListener(TopicSocketMsg.class,new TopicMsgListener());
        socketManager.addListener(SocketMsg.class,new SessionMessageListener((SessionClientStorage) socketManager.getClientStorage()));
    }

    @Bean
    @ConditionalOnMissingBean
    public SessionDisconnectListener sessionDisconnectListener(){
        return new SessionDisconnectListener();
    }
}
